<template>
  <div class="page-container">
    <div class="control-container">
      <div class="right-panel">
        <div class="search-panel">
          <el-form :inline="true" label-width="100">
            <el-form-item label="编号">
              <el-input v-model="query.num" />
            </el-form-item>
            <el-form-item label="名称">
              <el-input v-model="query.name" />
            </el-form-item>
            <el-button type="primary" @click="handleSelect">查询</el-button>
            <el-button type="primary" @click="resetForm()">重置</el-button>
            <el-button v-if="hasBtn('department:add')" type="primary" @click="handleAddOrUpdate('add')">添加</el-button>
          </el-form>
        </div>
        <div class="table-panel">
          <el-table
            v-loading="tableLoading"
            class="grid-data-table"
            height="calc(100vh - 400px)"
            :data="tableData"
          >
            <el-table-column align="center" type="index" width="80" />
            <el-table-column prop="num" label="编号" />
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="addressDetails" label="地址" width="380" />
            <el-table-column v-if="hasBtn('department:edit')" label="操作" width="150">
              <template slot-scope="scope">
                <el-button class="table-inner-btn" type="primary" size="small" @click="handleAddOrUpdate('update',scope.row)">修改</el-button>
                <el-button v-if="hasBtn('department:del')" class="table-inner-btn" type="primary" size="small" @click="handleDel(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <Pagination
              v-show="query.total>0"
              :total="query.total"
              :page.sync="query.page"
              :limit.sync="query.limit"
              @pagination="initData"
            />
          </div>
        </div>
      </div>
    </div>
    <AddProjectDepartmentDialog
      :dialog-visible="dialogVisible"
      :dialog-type="dialogType"
      :obj="obj"
      @closeDialog="closeDialog"
      @flushPage="flushPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getPage, del } from '@/api/info/projectDepartment'
import { hasBtnPermission as hasBtn } from '@/utils/buttons'
import { getDictLabel, parseTime } from '@/utils'
import AddProjectDepartmentDialog from '@/views/system/projectDepartment/components/AddProjectDepartmentDialog'

export default {
  name: 'ProjectDepartment',
  components: {
    AddProjectDepartmentDialog,
    Pagination
  },
  data() {
    return {
      tableLoading: false,
      query: {
        page: 1, limit: 10, total: 0,
        num: null, name: null
      },
      tableData: [],
      dialogVisible: false,
      reportDialogVisible: false,
      dialogType: 'detail',
      obj: null
    }
  },
  created() {
    this.initData()
  },
  methods: {
    getDictLabel, parseTime, hasBtn,
    initData() {
      const query = JSON.parse(JSON.stringify(this.query))
      this.tableLoading = true
      getPage(query).then(result => {
        this.tableLoading = false
        this.query.total = result.data.total
        this.tableData = result.data.list
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([row.id]).then(result => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleSelect()
        }).catch(() => {
        })
      }).catch(() => {
      })
    },
    resetForm() {
      this.query = {
        ...this.query,
        num: null, name: null
      }
    },
    // 查询
    handleSelect() {
      this.query.page = 1
      this.initData()
    },
    // 添加
    handleAddOrUpdate(dialogType, row) {
      this.dialogVisible = true
      this.dialogType = dialogType
      this.obj = row
    },
    flushPage() {
      this.clearQuery()
      this.initData()
    },
    clearQuery() {
      this.query = {
        page: 1, limit: 10, total: 0,
        num: null, name: null
      }
    },
    closeDialog() {
      this.dialogVisible = false
      this.reportDialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
